<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/30
 * Time: 10:13
 */
namespace app\index\controller;


use think\Db;

class Apply extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 申请结算首页
	 * @param string get.date 申请结算的时间
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('apply')
			->field('business.name,apply.date,apply.status,apply.id,business.linkman,business.email')
			->join('business','business.id = apply.bid','LEFT')
			->group('apply.date');
			//->where('apply.status',1);
		if(!empty(trim(input('get.date')))){
			$date = trim(input('get.date'));
			$info = explode('-',$date);
			$timestamp = getStartAndEndUnixTimestamp($info[0],$info[1],$info[2]);
			$db->where('apply.date','>',$timestamp['start'])->where('date','<',$timestamp['end']);
			$where['query']['date'] = $date;
		}
		if(!empty(trim(input('get.email')))){
			$email = trim(input('get.email'));
			$db->where('business.email','like',"%{$email}%");
			$where['query']['email'] = $email;
		}
		$result = $db->where('apply.status',1)->order('apply.id desc')->paginate(20,false,$where);
		return view('index',compact('result','date','email'));
	}
	
	/**
	 * 申请结算详情
	 * @param string get.date 申请结算的时间
	*/
	public function detail()
	{
		$where = ['query'=>[]];
		$date = trim(input('get.date'));
		$where['query']['date'] = $date;
		$result = Db::table('postpayment')
				->field('product.pname,postpayment.ordernumber,postpayment.date,product.purchaseprice,
					apply.status,apply.id,apply.number as anumber')
				->join('product','product.id = postpayment.pid','LEFT')
				->join('apply','apply.postid = postpayment.id','RIGHT')
				->where('apply.date',$date)
				->paginate(8,false,$where);
		return view('detail',compact('result'));
	}
	
	/**
	 * 结算
	 * @param string post.id 申请结算id
	*/
	public function settlement()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('apply')->where('id',$id)->update(['status'=>2]);
				if(!$result){
					throw new \PDOException('结算失败');
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 导出excel
	 * @param string get.email 邮箱
	*/
	public function excel()
	{
		$name = '结算表';
		$header = ['申请人邮箱','商品名称','单价(元)','应结算数量(个)'];
		$b = 0;
		$email = input('get.email');
		$data = Db::table('apply')
			->field('product.purchaseprice,apply.number,product.pname')
			->join('product','product.id = apply.pid','LEFT')
			->join('business','business.id = apply.bid','LEFT')
			->where('apply.status',1)->where('business.email',$email)
			->select();
		foreach($data as $val){
			$newdata[$b]['email']=$email;
			$newdata[$b]['pname']=$val['pname'];
			$newdata[$b]['purchaseprice']=$val['purchaseprice'];
			$newdata[$b]['number']=$val['number'];
			$b++;
		}
		excelExport($name,$header,$newdata);
	}
}